package Server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.AbstractMap;

public class MessageForward implements Runnable {
    private DataInputStream dataInputStream;
    private DataOutputStream dataOutputStream;
    private int userID;
    private boolean isClose;

    public MessageForward(Socket connSocket) {
        try {
            this.dataInputStream = new DataInputStream(connSocket.getInputStream());
            this.dataOutputStream = new DataOutputStream(connSocket.getOutputStream());
            this.isClose = false;
            this.userID = dataInputStream.readInt();
        } catch (IOException e) {
            this.isClose = true;
            System.out.println(e);
            try {
                this.dataInputStream.close();
                this.dataOutputStream.close();
                ServerMain.connSocketList.remove(this);
                System.out.println(e);
            } catch (IOException ce) {
                System.out.println(ce);
            }
        }
    }
    public int getUserID() {
        return this.userID;
    }
    public class Pair<K, V> extends AbstractMap.SimpleEntry<K, V> {
        public Pair(K key, V value) {
            super(key, value);
        }
    }
    private Pair<Integer, String> receive() {
        String msg = "";
        Integer roomID = -1;
        Pair<Integer, String> pair = null;
        try {
            roomID = dataInputStream.readInt();
            msg = dataInputStream.readUTF();
            pair = new Pair<Integer,String>(roomID, msg);
        } catch (IOException e) {
            this.isClose = true;
            System.out.println(e);
            try {
                this.dataInputStream.close();
                this.dataOutputStream.close();
                ServerMain.connSocketList.remove(this);
                System.out.println(e);
            } catch (IOException ce) {
                System.out.println(ce);
            }
        }
        return pair;
    }
    private void send(int roomID, String msg) {
        try {
            this.dataOutputStream.writeInt(roomID);
            this.dataOutputStream.writeUTF(msg);
        } catch (IOException e) {
            this.isClose = true;
            System.out.println(e);
            try {
                this.dataInputStream.close();
                this.dataOutputStream.close();
                ServerMain.connSocketList.remove(this);
                System.out.println(e);        
            } catch (IOException ce) {
                System.out.println(ce);
            }
        }
    }
    private void forward() {
        Pair<Integer, String> pair = this.receive();
        String msg = pair.getValue();
        int roomID = pair.getKey();

        /*

        user_ids = dao.getUserIDs(roomID);

        for theOther in Server.list:
            for user_id in user_ids:
                if theOther.getUserID == user_id:
                    theOther.send(roomID, msg)
        */
    }
    public void run() {
        while(!this.isClose) {
            forward();
        }
    }
}
